package com.guragura.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.FadeIn;
import com.badlogic.gdx.scenes.scene2d.actions.Sequence;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.guragura.Guragura;
import com.guragura.util.Constants;
import com.guragura.util.GuraScreenInputListener;
import com.guragura.widget.Dialog;
import com.guragura.widget.ExitDialog;
import com.guragura.widget.HighScorePanel;
import com.guragura.widget.ScorePanel;

/**
 * Main menu for this game
 * 
 * @author hartono
 * 
 */
public class MainMenuScreen extends GuraScreen {

	public Texture mainMenuTex;
	public TextureRegion ikanPari;

	public boolean isExitDialog = false;
	public boolean isScorePanel = false;

	private Stage stage;
	private Dialog exitDialog;
	private HighScorePanel highScorePanel;
	private ScorePanel scorePanel;

	private Button playButton;
	private Button creditButton;
	private Button helpButton;
	private Button settingButton;
	private Button exitButton;

	private Image background;

	int i = 0;

	public MainMenuScreen() {
		super();
	}

	public MainMenuScreen(Guragura guragura) {
		super(guragura);
	}

	@Override
	public void initScreen() {
		state = SCREEN_LOADING;
		stage = new Stage(320, 480, true);
		inputListener = new GuraScreenInputListener(camera, stage);

	}

	@Override
	public void onScreenRunning() {
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30.0f));
		stage.draw();
	}

	int c = 0;

	@Override
	public void onScreenPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScreenLoading() {

	}

	@Override
	public void show() {
		super.show();
		guragura.gameState = Guragura.MAIN_MENU;

		// maunya sih action sok2 gitu biar fade in, tapi kayaknya gagal
		background.action(Sequence.$(Delay.$(FadeIn.$(10f), 1f)));
		playButton.action(Sequence.$(Delay.$(FadeIn.$(10f), 1f)));

		guragura.music.play();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
		guragura.music.play();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
		guragura.music.stop();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		guragura.music.stop();
	}

	@Override
	public void initFromRes() {
		// TODO Auto-generated method stub

		playButton = new Button(guragura.atlasInterface.findRegion(
				Constants.RUN_BUTTON, 1), guragura.atlasInterface.findRegion(
				Constants.RUN_BUTTON, 2));
		// creditButton = new Button(guragura.atlasInterface.findRegion(
		// Constants.RUN_BUTTON, 1), guragura.atlasInterface.findRegion(
		// Constants.RUN_BUTTON, 2));
		helpButton = new Button(guragura.atlasInterface.findRegion(
				Constants.HELP_BUTTON, 1), guragura.atlasInterface.findRegion(
				Constants.HELP_BUTTON, 2));
		settingButton = new Button(guragura.atlasInterface.findRegion(
				Constants.OPTION_BUTTON, 1),
				guragura.atlasInterface.findRegion(Constants.OPTION_BUTTON, 2));
		exitButton = new Button(guragura.atlasInterface.findRegion(
				Constants.EXIT_BUTTON, 1), guragura.atlasInterface.findRegion(
				Constants.EXIT_BUTTON, 2));

		background = new Image(
				guragura.atlasInterface.findRegion(Constants.MAIN_MENU_SCREEN));

		background.x = 0;
		background.y = 0;

		playButton.x = 320 - playButton.width;
		exitButton.x = playButton.x;
		helpButton.x = playButton.x;
		settingButton.x = playButton.x;

		int h = 0;

		exitButton.y = h;
		settingButton.y = exitButton.y + exitButton.height + 10;

		helpButton.y = settingButton.y + settingButton.height + 10;
		playButton.y = helpButton.y + helpButton.height + 10;

		playButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				guragura.vibrator.vibrate();
				guragura.playGame();
			}
		});

		helpButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				guragura.showHelp();
			}
		});

		settingButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				guragura.showSetting();
			}
		});

		
		// credit button
		// creditButton.setClickListener(new ClickListener() {
		//
		// @Override
		// public void click(Actor arg0, float arg1, float arg2) {
		// // TODO Auto-generated method stub
		// guragura.showCredit();
		// }
		// });

		exitButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				exitScreen();
			}
		});

		stage.addActor(background);
		stage.addActor(playButton);
		stage.addActor(helpButton);
		stage.addActor(settingButton);
		stage.addActor(exitButton);

		exitDialog = new ExitDialog(320, 480, guragura, true);
		exitDialog.x = (320 - exitDialog.width) / 2;
		exitDialog.y = (480 - exitDialog.height) / 2;
		// stage.addActor(exitDialog);

		highScorePanel = new HighScorePanel(320, 480, guragura);
		scorePanel = new ScorePanel(320, 480, guragura);		 

		state = SCREEN_RUNNING;
	}

	@Override
	public void exitScreen() {
		// TODO Auto-generated method stub
		if (isExitDialog) {
			stage.removeActor(exitDialog);
			isExitDialog = false;
		} else {
			stage.addActor(exitDialog);
			isExitDialog = true;
		}

	}

	/**
	 * akan dipanggil di {@link Guragura} untuk menampilkan jendela score
	 * @param newHs apakah highscore dipecahkan atau tidak
	 * @param score score baru
	 * @param prevscore highscore sebelumnya
	 */
	public void showScorePanel(boolean newHs, int score,int prevscore) {
		if (!isScorePanel) {
			isScorePanel = true;
			if (newHs){						
				highScorePanel.setScore(score, prevscore);
				stage.addActor(highScorePanel);
			} else {
				scorePanel.setScore(score);
				stage.addActor(scorePanel);
			}
		}
	}

	/**
	 * akan dipanggil di {@link HighScorePanel} atau {@link ScorePanel} untuk menyembunyikan jendela score
	 */
	public void hideScorePanel() {
		if (isScorePanel) {
			isScorePanel = false;
			try {
				stage.removeActor(highScorePanel);
			} catch (Exception e) {

			}			
			try {
				stage.removeActor(scorePanel);
			} catch (Exception e) {

			}
		}
	}

}
